import model.ListNode;

public class AddTwoNumbers {
    public ListNode code(ListNode l1, ListNode l2) {
        ListNode newHead = new ListNode(0);
        ListNode prev = newHead;

        ListNode cur1 = l1, cur2 = l2;
        int sum = 0;

        while (cur1 != null || cur2 != null || sum != 0){
            if (cur1 != null){
                sum += cur1.val;
                cur1 = cur1.next;
            }

            if (cur2 != null){
                sum += cur2.val;
                cur2 = cur2.next;
            }

            prev.next = new ListNode(sum % 10);
            prev = prev.next;
            sum /= 10;
        }

        return newHead.next;
    }
}

